{extend name="public:_base" /}
{block name="main"}
<div class="page-container">

    <div class="Huialert Huialert-info">
        <i class="Hui-iconfont">&#xe6a6;</i>
        <h4 title="提示相关设置操作时应注意的要点">操作提示</h4>
        <ul>
            <li>数据备份功能根据你的选择备份全部数据或指定数据，导出的数据文件可用“数据恢复”功能或 phpMyAdmin 导入</li>
            <li>建议定期备份数据库</li>
            <li>注意访客统计表“visit_summary”和操作日志表“web_log_all”数据量比较大备份恢复较为耗时，用户按需求选择</li>
        </ul>
    </div>

    {include file="bak/bar" /}

    <div class="mDiv">
        <div class="ftitle">
            <h3>数据库表列表</h3>
            <h5>(共{$tableNum}条记录，共计{$total})</h5>
        </div>

        <div title="刷新数据" class="pReload"><i class="fa fa-refresh"></i></div>
    </div>

    <div class="mt-10">

        <form method="post" id="export-form" action="{:url('bak/export')}">

            <table id="tb_flexigrid" class="layui-table">
                <thead>
                    <tr class="text-c">
                        <th width="40"><input name="" type="checkbox" value=""></th>
                        <th width="40">序号</th>
                        <th>数据库表</th>
                        <th width="80">记录条数</th>
                        <th width="100">占用空间</th>
                        <th width="120">编码</th>
                        <th width="120">创建时间</th>
                        <th width="80" class="hidden-xs">备份状态</th>
                        <th width="120">操作</th>
                    </tr>
                </thead>

                <tbody>
                    {empty name="list"}
                    <tr>
                        <td class="no-data" align="center" axis="col0" colspan="50">
                            <i class="fa fa-exclamation-circle"></i>没有符合条件的记录
                        </td>
                    </tr>
                    {else/}
                    {volist name="list" id="vo" empty="$_html_empty"}
                    <tr class="text-c va-m" data-id="{$vo.Name}">
                        <td><input name="tables[]" type="checkbox" value="{$vo.Name??''}"></td>
                        <td>{$i}</td>
                        <td>
                            <div>{$vo.Name}</div>
                        </td>
                        <td>
                            <div class="w80 tc">{$vo.Rows}</div>
                        </td>
                        <td>
                            <div class="w80 tc">{$vo.Data_length|format_bytes}</div>
                        </td>
                        <td>
                            <div class="w120 tc">{$vo.Collation}</div>
                        </td>
                        <td>
                            <div class="w160 tc">{$vo.Create_time}</div>
                        </td>
                        <td>
                            <div class="info w80 tc">未备份</div>
                        </td>
                        <td>
                            <div class="w160 tc">
                                <div class="w160 tc">
                                    <a style="text-decoration:none" class="btn radius btn-xs btn-primary"
                                        onClick="postbak('{:url('bak/optimize')}','{$vo.Name}')" href="javascript:;"
                                        title="优化"><i class="fa fa-magic"></i>优化</a>
                                    <a style="text-decoration:none" class="btn radius btn-xs btn-primary"
                                        onClick="postbak('{:url('bak/repair')}','{$vo.Name}')" href="javascript:;"
                                        title="修复"><i class="fa fa-magic"></i>修复</a>

                                    <!-- <a href="{:url('bak/optimize',['tablename'=>$vo.Name])}" class="btn radius blue"><i
                                            class="fa fa-magic"></i>优化</a>
                                    <a href="{:url('bak/repair',['tablename'=>$vo.Name])}" class="btn radius green" href=""><i
                                            class="fa fa-wrench"></i>修复</a> -->
                                </div>
                            </div>
                        </td>

                    </tr>
                    {/volist}

                    {/empty}
                </tbody>
            </table>

        </form>

    </div>

    <div class="fbutton">
        <a class="bak-update">
            <div class="btn radius btn-danger" title="数据备份">
                <span><i class="Hui-iconfont Hui-iconfont-down"></i><span class="export">点击数据备份</span></span>
            </div>
        </a>
    </div>

</div>
{/block}

{block name="js"}
<script>
    $(document).ready(function () {
        // 表格行点击选中切换
        $('#tb_flexigrid >tbody >tr').click(function () {
            $(this).toggleClass('trSelected');
        });
    });

    /**
     * 提交
     * @param url
     * @param id
     */
    function postbak(url, tablename) {
        $.post(url, { tablename: tablename }, function (data) {
            //没有权限返回信息
            if (data.status == '0') {
                layer.alert(data.msg);
            }
            if (data.status == "1") {
                layer.alert(data.msg);
                layer_close();
            }
        }, 'json')
    }

    (function ($) {
        var $form = $("#export-form"), $export = $(".export"), tables
        $export.click(function () {
            if ($("input[name^='tables']:checked").length == 0) {
                layer.alert('请选中要备份的数据表', { icon: 2 });
                return false;
            }
            $export.addClass("disabled");
            $export.html("正在发送备份请求...");
            $.post(
                $form.attr("action"),
                $form.serialize(),
                function (data) {
                    if (data.status) {
                        tables = data.tables;
                        var loading = layer.msg('初始化成功，请不要关闭本页面！',
                            {
                                icon: 1,
                                time: 3600000, //1小时后后自动关闭
                                shade: [0.2] //0.1透明度的白色背景
                            });
                        $export.html(data.info + "开始备份，请不要关闭本页面！");
                        backup(data.tab);
                        window.onbeforeunload = function () { return "正在备份数据库，请不要关闭！" }
                    } else {
                        layer.alert(data.info, { icon: 2 });
                        $export.removeClass("disabled");
                        $export.html("立即备份");
                    }
                },
                "json"
            );
            return false;
        });

        function backup(tab, status) {
            status && showmsg(tab.id, "开始备份……(0%)");
            $.post($form.attr("action"), tab, function (data) {
                if (data.status) {
                    if (tab.table) {
                        showmsg(tab.id, data.info);
                        var loading = layer.msg('正在备份表(' + tab.table + ')……' + tab.speed + '%，请不要关闭本页面！',
                            {
                                icon: 1,
                                time: 3600000, //1小时后后自动关闭
                                shade: [0.2] //0.1透明度的白色背景
                            });
                        $export.html('初始化成功！正在备份表(' + tab.table + ')……' + tab.speed + '%，请不要关闭本页面！');
                    } else {
                        $export.html('初始化成功！开始备份……，请不要关闭本页面！');
                    }
                    if (!$.isPlainObject(data.tab)) {
                        var loading = layer.msg('备份完成……100%，请不要关闭本页面！',
                            {
                                icon: 1,
                                time: 2000, //1小时后后自动关闭
                                shade: [0.2] //0.1透明度的白色背景
                            });
                        $export.removeClass("disabled");
                        $export.html("备份完成……100%，点击重新备份");
                        setTimeout(function () {
                            layer.closeAll();
                            layer.alert('备份成功！', { icon: 6 });
                        }, 1000);
                        window.onbeforeunload = function () { return null }
                        return;
                    }
                    backup(data.tab, tab.id != data.tab.id);
                } else {
                    layer.closeAll();
                    $export.removeClass("disabled");
                    $export.html("立即备份");
                }
            }, "json");
        }

        function showmsg(id, msg) {
            $form.find("input[value=" + tables[id] + "]").closest("tr").find(".info").html(msg);
        }
    })(jQuery);
</script>
{/block}